<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />

    <BaseDialog
      :visible.sync="isselectVin"
      :title="'车辆列表'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <vinSelect   @close="closeHandler" @selectedFun="selectedFun"/>
    </BaseDialog>
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <!-- <el-button @click="closeDrawer(2)" type="primary">保 存</el-button> -->
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">保 存</multiuse-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 3)">确 认</multiuse-button>
      <!-- <el-button @click="closeDrawer(3)" type="primary">确 认</el-button> -->
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import vinSelect from './vinSelect'
import {getDetail,postSave,getWarehouseList} from '@/api/omSale/HeadquartersInventory/VehicleTransfer/index.js'
import { dictionary } from '@/utils/dictMixin';

export default {
  data(){
    this.saveData=null;
    return{
      formList:this.createdFrom(),
      isselectVin:false,
      vehicleId:''
    }
  },
  components: {
    vinSelect
  },
  props:['rowData'],
  created(){
    this.getDetail(this.rowData);


  },
  mixins:[dictionary],
  methods:{
   createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '移库单号',
          fieldName: 'transferNo',
          placeholder: '请输入',
          disabled:true,
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请选择',
          readonly:true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          },
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '移库人',
          fieldName: 'transferPersonName',
          placeholder: '请输入',
          disabled:true,
        },
        {
          type: 'SELECT',
          label: '原仓库名称',
          fieldName: 'fromWarehouse',
          placeholder: '请选择',
          disabled:true,
          itemList:[]
        },
        {
          type: 'SELECT',
          label: '新仓库名称',
          fieldName: 'toWarehouse',
          placeholder: '请选择',
          itemList:[],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
        {
          type: 'DATE',
          label: '移库时间',
          fieldName: 'transferDate',
          placeholder: '请输入',
          disabled:true,
          initialValue:moment().format('YYYY-MM-DD HH:mm:ss')
        },
        {
          type: 'TEXT_AREA',
          label: '移库原因',
          fieldName: 'transferReason',
          selfCols:3
        },

      ]
    },
    seacherFun(){
      this.isselectVin=true;
    },
    async getDetail(data){
      let res=await getDetail({transferId:data.id})
      let res1=await getWarehouseList({vin:res.data.vehicleId})
      this.formList.find(item => item.fieldName === 'fromWarehouse').itemList = res1.data.list.map(x=>({text:x.warehouseName,value:x.warehouseId}));
      this.formList.find(item => item.fieldName === 'toWarehouse').itemList = res1.data.list.map(x=>({text:x.warehouseName,value:x.warehouseId}));
      this.warehouseList=res1.data.list.map(x=>({text:x.warehouseName,value:x.warehouseId}));
      for (let i in res.data) {
      this.formList.map(x => {
          if (i == x.fieldName) {
            x.initialValue = res.data[i];
          }
        });
      };
      this.vehicleId=res.data.vehicleId;
      let arr=this.warehouseList;
      let array=[]
      arr.map(x=>{
        if(x.value!=res.data.fromWarehouse){
          array.push({text:x.text,value:x.value})
        }
      })
      this.formList.find(item => item.fieldName == 'toWarehouse').itemList =array;
    },
    changeHandle(val){
      this.saveData=val;
    },
    closeHandler(){
      this.isselectVin=false;
    },
    async selectedFun(val){
      this.isselectVin=false;
      let json=val[0]
      for (let i in json) {
      this.formList.map(x => {
          if (i == x.fieldName) {
            x.initialValue = json[i];
          }
        });
      }
      this.formList.find(item => item.fieldName == 'fromWarehouse').initialValue=json.warehouseId;
      this.vehicleId=json.vehicleId
      let res=await getWarehouseList({vin:json.vehicleId})
      this.formList.find(item => item.fieldName == 'fromWarehouse').itemList = res.data.list.map(x=>({text:x.warehouseName,value:x.warehouseId}));
      this.formList.find(item => item.fieldName == 'toWarehouse').itemList = res.data.list.map(x=>({text:x.warehouseName,value:x.warehouseId}));
      this.warehouseList=res.data.list.map(x=>({text:x.warehouseName,value:x.warehouseId}));
      let arr=this.warehouseList;
      let array=[]
      arr.map(x=>{
        if(x.value!=json.warehouseId){
          array.push({text:x.text,value:x.value})
        }
      })
      this.formList.find(item => item.fieldName == 'toWarehouse').initialValue ='';
      this.formList.find(item => item.fieldName == 'toWarehouse').itemList =array;
    },

    async closeDrawer(type){
      if(type==1){
        this.$emit('close')
      }else if(type==2){
        this.$refs.from.SUBMIT_FORM();
        console.log(this.saveData, 'this.saveData');
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          json.vehicleId=this.vehicleId;
          json.id=this.rowData.id;
          json.transferStatus=60391002
          let res = await postSave(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          }else{
            this.saveData=null;
          }
        }

      }else{
        await this.$confirm('是否确认移库?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        }).then(async () => {
          this.$refs.from.SUBMIT_FORM();
          console.log(this.saveData, 'this.saveData');
          if (this.saveData) {
            let json = JSON.parse(JSON.stringify(this.saveData));
            json.vehicleId=this.vehicleId;
            json.id=this.rowData.id;
            json.transferStatus=60391001
            let res = await postSave(json);
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '确认成功',
                type: 'success'
              });
              this.$emit('close', json);
            }else{
              this.saveData=null;
            }
          }
        })
        .catch(()=>{})

      }
    }

  }
}
</script>
